package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.RMain;
import cn.acewill.yxd.yunxundian.model.tempLostRR;
import cn.acewill.yxd.yunxundian.ui.sign.YxdMajorMainActivity;

/**
 * Created by AAA on 2017/4/21.
 */

public class YxdMinorMainAdpter extends BaseAdapter{
    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    private Context context;
    private ArrayList<RMain.TablesBean.RowsBean.Q2Bean> list;
    private List<RMain.TablesBean.RowsBean.Q2Bean> checkList;

    public YxdMinorMainAdpter(Context context, ArrayList<RMain.TablesBean.RowsBean.Q2Bean> list,List<RMain.TablesBean.RowsBean.Q2Bean> checkList){
        this.context=context;
        this.list=list;
        this.checkList = checkList;
    }
    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.yxd_minoritem_info,parent,false);
            holder=new ViewHolder();
            holder.minorItem= (TextView) convertView.findViewById(R.id.minorItem);
            holder.isPoint= (ImageView) convertView.findViewById(R.id.isPoint);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        RMain.TablesBean.RowsBean.Q2Bean bean=list.get(position);
        if(list.get(position)!=null){
            holder.minorItem.setText(bean.getSName()+"\t"+bean.getReportRowName());
            // TODO 此处是测试，记得修改
            if (bean.getIsPoint()>=2){
                //如果填写了就去掉标记
                holder.isPoint.setVisibility(View.VISIBLE);
                try {
                    List<tempLostRR> rr = db.selector(tempLostRR.class).where("schId","=", YxdMajorMainActivity.schid).findAll();
                    if (rr!=null&&rr.size()>0){
                        for (tempLostRR lostRR:rr){
                            if (lostRR.getRRowId()==list.get(position).getID()){
                                holder.isPoint.setVisibility(View.GONE);
                            }
                        }
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }else{
            holder.isPoint.setVisibility(View.GONE);
            }
            if (checkList!=null&&checkList.size()>0){
                int count = 0;
                for (RMain.TablesBean.RowsBean.Q2Bean q2:checkList){
                    if (q2.getID() == bean.getID()){
                        count++;
                    }
                }
                if (count>0){
                    holder.minorItem.setBackgroundColor(ContextCompat.getColor(context,R.color.pink_light));
                }else{
                    holder.minorItem.setBackgroundColor(ContextCompat.getColor(context,R.color.white));
                }
                Log.e("count------",count+"");
            }
        }else{
            holder.minorItem.setText("暂无数据");
        }

        return convertView;
    }

    class ViewHolder{
        TextView minorItem;
     //   RoatTextView mytext;
        ImageView isPoint;
    }

}
